##########################################################################################

library(dplyr)
library(ggplot2)
library(data.table)
library(RColorBrewer)
library(optparse)

##########################################################################################

option_list <- list(
    make_option(c("--maf_file"), type = "character") ,
    make_option(c("--images_path"), type = "character") ,
    make_option(c("--recurrent_point_file"), type = "character") ,
    make_option(c("--info_file"), type = "character")
)

if(1!=1){
    
    work_dir <- "~/20220915_gastric_multiple/dna_combine/"
    maf_file <- paste(work_dir,"/maf/All_GGA.all.maf",sep="")
	images_path <- paste(work_dir,"/images/mutRate",sep="")
	info_file <- paste(work_dir,"/config/tumor_normal.class.list",sep="")
    recurrent_point_file <- paste(work_dir,"/images/mutRate/MutRate.RecurrentPoint.tsv",sep="")

}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

maf_file <- opt$maf_file
info_file <- opt$info_file
images_path <- opt$images_path
recurrent_point_file <- opt$recurrent_point_file

dir.create(images_path , recursive = T)

###########################################################################################

info <- data.frame(fread(info_file))
dat_maf <- data.frame(fread( maf_file ))
dat_point <- data.frame(fread( recurrent_point_file ))

###########################################################################################

Variant_Type <- c("Missense_Mutation","Nonsense_Mutation","Frame_Shift_Ins","Frame_Shift_Del","In_Frame_Ins","In_Frame_Del","Splice_Site","Nonstop_Mutation")

###########################################################################################

maf <- dat_maf[dat_maf$t_alt_count>0,]
maf_use <- data.frame(
    Hugo_Symbol = maf$Hugo_Symbol, 
    Chromosome = maf$Chromosome , Start_Position =  maf$Start_Position , End_Position = maf$End_Position ,
    Reference_Allele = maf$Reference_Allele , Tumor_Seq_Allele2 = maf$Tumor_Seq_Allele2 , 
    Variant_Classification = maf$Variant_Classification , Tumor = maf$Tumor_Sample_Barcode  )

maf_use <- subset( maf_use , Variant_Classification %in% Variant_Type )
maf_use <- merge( maf_use , info , by = "Tumor" )

###########################################################################################
## 位点
maf_use$vid <- paste(maf_use$Hugo_Symbol , 
    maf_use$Chromosome , maf_use$Start_Position , 
    maf_use$Reference_Allele , maf_use$Tumor_Seq_Allele2 , sep = ":")

maf_use <- subset( maf_use , vid %in% dat_point$vid )

###########################################################################################
## 计算每个突变位点是否共享
result <- c()
for( normal in unique(maf_use$Normal) ){
    tmp <- subset( maf_use , Normal == normal )

    for( mut in unique(tmp$vid) ){
        tmp2 <- subset( tmp , vid == mut )
        share <- length(grep( "IM" , unique(tmp2$Class))) > 0 & length(grep( "GC" , unique(tmp2$Class))) > 0
        if(share){
            tmp2$Share <- TRUE
        }else{
            tmp2$Share <- FALSE
        }
        result <- rbind( result , tmp2 )
    }
}

###########################################################################################
## 计算每个基因在多少人中为共享
result_gene <- c()

for( geneN in unique(result$vid) ){

    tmp <- subset( result , vid == geneN )
    tmp_IM <- subset( tmp , Class == "IM" )
    tmp_GC <- subset( tmp , Class %in% c("IGC" , "DGC") )
    tmp_IGC <- subset( tmp , Class %in% c("IGC" ) )
    tmp_DGC <- subset( tmp , Class %in% c("DGC") )

    shareSample <- unique(tmp[tmp$Share=="TRUE","Normal"])
    shareSample_IGC <- unique(tmp[tmp$Share=="TRUE" & tmp$Class %in% c("IGC" ) ,"Normal"])
    shareSample_DGC <- unique(tmp[tmp$Share=="TRUE" & tmp$Class %in% c("DGC" ) ,"Normal"])

    uniqueSample <- unique(tmp[tmp$Share!="TRUE","Normal"])
    ## 有样本一个基因多个突变，有突变是共享的，这种样本判断为这个基因共享突变
    uniqueSample <- uniqueSample[!(uniqueSample %in% shareSample )]
    ## 癌前特有的样本
    uniqueSample_IM <- uniqueSample[ uniqueSample %in% tmp_IM$Normal ]
    ## 癌特有的样本
    uniqueSample_GC <- uniqueSample[ uniqueSample %in% tmp_GC$Normal ]
    uniqueSample_IGC <- uniqueSample[ uniqueSample %in% tmp_IGC$Normal ]
    uniqueSample_DGC <- uniqueSample[ uniqueSample %in% tmp_DGC$Normal ]

    tmp_res <- data.frame( Hugo_Symbol = geneN , 
        ShareClass = c(
            "IM_U" , "IM_S" , "GC_U" , "GC_S" ,
            "IGC_U" , "IGC_S" , "DGC_U" , "DGC_S"),
        MutSampleNum = c(length(uniqueSample_IM) , length(shareSample) , length(uniqueSample_GC) , length(shareSample) , 
            length(uniqueSample_IGC) , length(shareSample_IGC) , length(uniqueSample_DGC) , length(shareSample_DGC)  )
    )

    result_gene <- rbind( result_gene , tmp_res )
}

out_name <- paste0( images_path , "/MutShare.RecurrentPoint.tsv" )
write.table( result_gene , out_name , row.names = F , quote = F , sep = "\t" )